home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Jotto2.so Folder / Jotto ][ ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  4.9 KB  |  219 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Jotto ][ -=- a simple word game, revisited
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg menus.h"
  32. #include "msg sounds.h"
  33. #include "msg prefs.h"
  34. #include "msg environment.h"
  35. #include "jotto.h"
  36. #include "AppleEvents.h"
  37. #include "EPPC.h"
  38.  
  39. void main(void)
  40. {
  41.     MaxApplZone();    
  42.     InitGraf(&qd.thePort);
  43.     InitFonts();
  44.     FlushEvents(everyEvent, 0);
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(0L);
  49.     InitCursor();
  50.     GetDateTime((unsigned long *)&qd.randSeed);
  51.     
  52.     CheckEnvironment();    
  53.     DoIntegrityCheck();    
  54.     InitSounds();
  55.     PrefsError(PreferencesInit());
  56.     InitEnvironment();    
  57.     InitMSGGraphics();
  58.     InitGame();
  59.     EventLoop();    
  60.     ShutDownEnvironment();
  61.     ExitToShell();
  62. }
  63.  
  64. void EventLoop(void)
  65. {
  66.     EventRecord        theEvent;
  67.     
  68.     while (!gDone)
  69.     {
  70.         SetCursor(&qd.arrow);
  71.         HiliteMenu(0);
  72.         
  73.         if(gMainWindow)
  74.             SetPort(gMainWindow);
  75.         
  76.         if (WaitNextEvent(everyEvent, &theEvent, 0, 0L))
  77.             DispatchEvents(theEvent);
  78.     }
  79. }
  80.  
  81. void DispatchEvents(EventRecord theEvent)
  82. {
  83.     short                i;
  84.     OSErr            isHuman;
  85.     Point            thisPoint;
  86.     
  87.     switch (theEvent.what)
  88.     {
  89.         case mouseDown:
  90.             HandleMouseDown(theEvent);
  91.             break;
  92.         case keyDown:
  93.         case autoKey:
  94.             if(theEvent.modifiers & cmdKey)
  95.             {
  96.                 AdjustMenus();
  97.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  98.             }
  99.             else if (gMainWindow)
  100.                 GameKeyEvent((char)(theEvent.message & charCodeMask));
  101.             break;
  102.         case diskEvt:
  103.             if (HiWord(theEvent.message)!=noErr)
  104.             {
  105.                 DILoad();
  106.                 SetPt(&thisPoint, 120, 120);
  107.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  108.                 DIUnload();
  109.             }
  110.             break;
  111.         case updateEvt:
  112.             BeginUpdate((WindowPtr)theEvent.message);
  113.             
  114.             if((WindowPtr)theEvent.message == gMainWindow)
  115.                 UpdateBoard();
  116.             
  117.             for (i=0; i<NUM_HELP; i++)
  118.                 if ((WindowPtr)theEvent.message == gHelp[i])
  119.                     UpdateHelp(i);
  120.             
  121.             EndUpdate((WindowPtr)theEvent.message);
  122.             break;
  123.         case kHighLevelEvent:
  124.             if (gHasAppleEvents)
  125.                 AEProcessAppleEvent(&theEvent);
  126.             break;
  127.     }
  128. }
  129.  
  130. void HandleMouseDown(EventRecord theEvent)
  131. {
  132.     WindowPtr            theWindow;
  133.     short                    windowCode;
  134.     long                windSize;
  135.     GrafPtr                oldPort;
  136.     short                    i;
  137.     Rect                sizeRect;
  138.     
  139.     windowCode=FindWindow(theEvent.where, &theWindow);
  140.     switch (windowCode)
  141.     {
  142.         case inMenuBar:
  143.             AdjustMenus();
  144.             HandleMenu(MenuSelect(theEvent.where));
  145.             break;
  146.         case inContent:
  147.             if(FrontWindow() != theWindow)
  148.                 SelectWindow(theWindow);
  149.             else
  150.                 if(theWindow == gMainWindow)
  151.                     GameEvent();
  152.             break;
  153.         case inSysWindow:
  154.             SystemClick(&theEvent, theWindow);
  155.             break;
  156.         case inDrag:
  157.             DragWindow(theWindow, theEvent.where, &gDragRect);
  158.             if(theWindow == gMainWindow)
  159.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  160.             break;
  161.         case inGoAway:
  162.             if (TrackGoAway(theWindow, theEvent.where))
  163.             {
  164.                 for (i=0; i<NUM_HELP; i++)
  165.                     if (theWindow == gHelp[i])
  166.                         gHelp[i]=0L;
  167.                 
  168.                 if(theWindow == gMainWindow)
  169.                     CloseMainWindow();
  170.                 else
  171.                     DisposeWindow(theWindow);
  172.                 
  173.                 AdjustMenus();
  174.             }
  175.             break;
  176.         case inGrow:
  177.             sizeRect = qd.screenBits.bounds;
  178.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  179.             
  180.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  181.             if(windSize != 0)
  182.             {
  183.                 GetPort(&oldPort);
  184.                 SetPort(theWindow);
  185.                 EraseRect(&theWindow->portRect);
  186.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  187.                 InvalRect(&theWindow->portRect);
  188.                 SetPort(oldPort);
  189.             }
  190.             
  191.             if(theWindow == gMainWindow)
  192.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  193.             
  194.             break;
  195.         case inZoomIn:
  196.         case inZoomOut:
  197.             if(TrackBox(theWindow, theEvent.where, windowCode))
  198.             {
  199.                 GetPort(&oldPort);
  200.                 SetPort(theWindow);
  201.                 ZoomWindow(theWindow, windowCode, FALSE);
  202.                 InvalRect(&theWindow->portRect);
  203.                 SetPort(oldPort);
  204.             }
  205.             
  206.             if(theWindow == gMainWindow)
  207.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  208.             
  209.             break;
  210.     }
  211. }
  212.  
  213. void ShutDownEnvironment(void)
  214. {
  215.     ShutDownGame();
  216.     ShutDownMSGGraphics();
  217.     CloseSounds();    
  218. }
  219.